﻿function validateValueRange(obj, lowerBound, upperBound) {
    //   alert(lowerBound);
    var validateobj = document.getElementById(obj.id + '||validate');
    if (obj.value == '' || (isNumeric(obj.value) && isNumeric(lowerBound) && isNumeric(upperBound))) {
        if (parseFloat(obj.value) < parseFloat(lowerBound) || parseFloat(obj.value) > parseFloat(upperBound)) {
            if (validateobj != null) {
                validateobj.style.color = "#FF0000";
            }
            return false;  // value not in the range
        }
        else {
            if (validateobj != null) {
                validateobj.style.color = "#0B3861";
            }
            return true;
        }
    }
    else {
        if (validateobj != null) {
            validateobj.style.color = "#FF0000";
        }
        return false;
        //        event.cancelBubble = true;
        //        event.returnValue = false; // values are not numeric
    }
}

function validateValueGap(obj, gap) {
    var validateobj = document.getElementById(obj.id + '||validate');
    if (isNumeric(obj.value) && isNumeric(gap)) {
        if ((parseFloat(obj.value) % parseFloat(gap)) > 0) {
            if (validateobj != null) {
                validateobj.style.color = "#FF0000";
            }
            return false; // value is not a multiplication of the gap
        }
        else {
            if (validateobj != null) {
                validateobj.style.color = "#0B3861";
            }
            return true;
        }
    }
    else {
        if (validateobj != null) {
            validateobj.style.color = "#FF0000";
        }
        return false; // value or gap not numeric
    }
}

function trim(value) {
    return value.toString().replace(/(^\s*)|(\s*$)/g, "");
}

function isInteger(num) {
    if (trim(num) == parseInt(num)) {
        return true;
    } else {
        return false;
    }
}

function isFloat(num) {
    if (trim(num) == parseFloat(num)) {
        return true;
    } else {
        return false;
    }
}

function isNumeric(num) {
    if (isInteger(num) || isFloat(num)) {
        return true;
    } else {
        return false;
    }
}

function validateCodeForSymbols(e) {
    //    var e = (window.event) ? window.event : evt;
    //    var AsciiCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
    //    if (((AsciiCode >= 33) && (AsciiCode <= 47)) || ((AsciiCode >= 58) && (AsciiCode <= 64)) || ((AsciiCode >= 91) && (AsciiCode <= 96)) || ((AsciiCode >= 123) && (AsciiCode <= 126))) {
    //        return false;
    //    }
    //    else {
    //        return true;
    //    }

    // Allowed: 0-9 [48-57], "." [46], "," [44], "<backspace>" [8], "<tab>" [9], 65-90 [A-Z], 97-122 [a-z], 95 "_"
    var charCode = (e.which) ? e.which : e.keyCode
    if (charCode != 9) {
        if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122)) {
            if (charCode == 46 || charCode == 44 || e.which == 0 || charCode == 95 || charCode == 32) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return true;
        }
    }
}

function handleNumbersOnly(e) {
    // Allowed: 0-9 [48-57], "." [46], "," [44], "<backspace>" [8], "<tab>" [9]
    var charCode = (e.which) ? e.which : e.keyCode
    if (charCode != 9) { //Allow tab
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            if (charCode == 46 || charCode == 44 || e.which == 0) {
                return true; //Valid Entry
            }
            else {
                return false; //Invalid Entry
            }
        }
        else {
            return true; //Valid entry
        }
    }
}

//Overloaded function copied from http://wiki.asp.net/page.aspx/1136/validation-of-numeric-values-on-keypress-event-using-javascript/
function handleNumbersOnly(e, obj, allowDecimal, allowNegative) {

    if (allowDecimal == null) { allowDecimal = true }
    if (allowNegative == null) { allowNegative = false }

    var key;
    var isCtrl = false;
    var keychar;
    var reg;

    if (window.event) {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey
    }
    else if (e.which) {
        key = e.which;
        isCtrl = e.ctrlKey;
    }

    if (isNaN(key)) return true;

    keychar = String.fromCharCode(key);

    // check for backspace or delete, or if Ctrl was pressed
    if (key == 8 || isCtrl) {
        return true;
    }

    reg = /\d/;
    var isFirstN;
    var isFirstD;

    if (obj == null) {
        isFirstN = allowNegative ? keychar == '-' : false;
        isFirstD = allowDecimal ? keychar == '.' : false;
    }
    else {
        isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
        isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
    }

    return isFirstN || isFirstD || reg.test(keychar);
}

function formatNumber(pnumber) {
    decimals = '2';
    if (isNaN(pnumber)) { return '0.00' };
    if (pnumber == '') { return '0.00' };

    var snum = new String(pnumber);
    var sec = snum.split('.');
    var whole = parseFloat(sec[0]);
    var result = '0.00';

    if (sec.length > 1) {
        var dec = new String(sec[1]);
        dec = String(parseFloat(sec[1]) / Math.pow(10, (dec.length - decimals)));
        dec = String(whole + Math.round(parseFloat(dec)) / Math.pow(10, decimals));
        var dot = dec.indexOf('.');
        if (dot == -1) {
            dec += '.';
            dot = dec.indexOf('.');
        }
        while (dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    } else {
        var dot;
        var dec = new String(whole);
        dec += '.';
        dot = dec.indexOf('.');
        while (dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    }
    return result;
}

function makeZeroWhenLeave(obj) {
    if (obj.value == '') {
        obj.value = '0.00';
    }
}

function toUpperCase(value) {
    return value.toString().toUpperCase();
}

function toLowerCase(value) {
    return value.toString().toLowerCase();
}

String.noLC = new Object
  ({ the: 1, a: 1, an: 1, and: 1, or: 1, but: 1, aboard: 1,
      about: 1, above: 1, across: 1, after: 1, against: 1,
      along: 1, amid: 1, among: 1, around: 1, as: 1, at: 1,
      before: 1, behind: 1, below: 1, beneath: 1, beside: 1,
      besides: 1, between: 1, beyond: 1, but: 1, by: 1, 'for': 1,
      from: 1, 'in': 1, inside: 1, into: 1, like: 1, minus: 1,
      near: 1, of: 1, off: 1, on: 1, onto: 1, opposite: 1,
      outside: 1, over: 1, past: 1, per: 1, plus: 1,
      regarding: 1, since: 1, than: 1, through: 1, to: 1,
      toward: 1, towards: 1, under: 1, underneath: 1, unlike: 1,
      until: 1, up: 1, upon: 1, versus: 1, via: 1, 'with': 1,
      within: 1, without: 1
  });

function toTitleCase(value) {
    var parts = value.toString().split(' ');
    if (parts.length == 0) return '';

    var fixed = new Array();
    for (var i in parts) {
        var fix = '';
        if (String.noLC[parts[i]]) {
            fix = toLowerCase(parts[i]);
        }
        else if (parts[i].match(/^([A-Z]\.)+$/i)) { // will mess up "i.e." and like
            fix = toUpperCase(parts[i]);
        }
        else if (parts[i].match(/^[^aeiouy]+$/i)) { // voweless words are almost always acronyms
            fix = toUpperCase(parts[i]);
        }
        else {
            fix = toUpperCase(parts[i].substr(0, 1)) +
                 parts[i].substr(1, parts[i].length);
        }
        fixed.push(fix);
    }
    fixed[0] = toUpperCase(fixed[0].substr(0, 1)) +
                 fixed[0].substr(1, fixed[0].length);
    return fixed.join(' ');
}

function toSentenceCase(value) {
    return toUpperCase(value.toString().substr(0, 1)) + toLowerCase(value.toString().substr(1, value.toString().length)) + '.';
}

function Nohandlekeys(e) {
    var charCode = (e.which) ? e.which : e.keyCode
    if (charCode == 9) {
        return true;
    }
    else {
        return false;
    }
}

function validateEmail(str) {//str is a string
    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if ((str.match(emailRegEx)) && (str != "")) {
        //If E-mail Address is ok.
        return true;
    } else {
        //If E-mail Address is invalid.
        return false;
    }
}

function handleDecimals(obj, noOfDecimals) {
    var num = new Number(obj.value);
    obj.value = num.toFixed(noOfDecimals);
}
